import random,time
from comm.style import *
class Ship():
    def __init__(self,shipdata) -> None:
            self.id     = shipdata[0]
            self.s_name   = shipdata[1]
            self.type   = shipdata[2]
            self.s_energy   = float(shipdata[3])
            self.s_vel   = float(shipdata[4])
            self.s_jumpVel  = float(shipdata[5])
            self.s_cap   = shipdata[6]
            self.out   = shipdata[7]
            self.mid   = shipdata[8]
            self.core   = shipdata[9]
            self.s_shield   = float(shipdata[10])
            self.s_armor   = float(shipdata[11])
            self.s_hull   = float(shipdata[12])
            self.s_shieldRecovery   = float(shipdata[13])
            self.cargoHold = {}

    # # 展示
    # def show(self):
    #     print(f"能量:{self.eng},钱数:{self.coin},等级:{self.lev},钻石:{self.jewel}.")
    # # 换燃料
    # def pls_eng(self,e):
    #     self.eng += e
    #     self.coin -= (e // 4) * 5
    #     self.eng += self.jewel * 2
    #     print(f"eng:{self.eng}")
    # # 获得金币
    # def pls_coin(self):
    #     if self.eng >= self.lev * 10:
    #         if self.lev <= 3:
    #             self.coin += random.randint(self.lev*1,self.lev*7)
    #         elif self.lev <= 5:
    #             self.coin += int(random.randint(self.lev*2,self.lev*5) * 1.5)
    #         elif self.lev <= 10:
    #             self.coin += random.randint(self.lev*2,self.lev*7) * 2
    #         self.eng -= self.lev * 10
    #         print(f"coin:{self.coin}")
    #     else:
    #         print("No eng!")
    # # 升级
    # def pls_lev(self):
    #     if (self.eng >= self.lev * 5) and (self.coin >= self.lev * 7):
    #         print('[',end = '')
    #         for i in range(20):
    #             print("░",end = '')
    #             time.sleep(0.06)
    #         print('] OK!',end = '')
    #         self.eng -= self.lev * 5
    #         self.coin -= self.lev * 7
    #         self.lev += 1
    #     else:
    #         print("Nothing!")
    # # 买钻石
    # def pls_jewel(self,g):
    #     self.jewel += g
    #     self.coin -= 10 * g
    # # 做题
    # def go(self):
    #     for i in range(1,self.lev*5+1):
    #         a = random.randint(1,99)
    #         b = random.randint(1,99)
    #         c = random.randint(1,3)
    #         if c == 1:
    #             print("{}+{}=".format(a,b),end = '')
    #             n = int(input())
    #             if n == a+b:
    #                 print("Yes")
    #                 self.eng += 2 + self.jewel * 2
    #             else:
    #                 print("No")
    #                 print(a+b)
    #         if c == 2:
    #             print("{}-{}=".format(a,b),end = '')
    #             n = int(input())
    #             if n == a-b:
    #                 print("Yes")
    #                 self.eng += 5 + self.jewel * 2
    #             else:
    #                 print("No")
    #                 print(a-b)
    #         if c == 3:
    #             print("{}*{}=".format(a,b),end = '')
    #             n = int(input())
    #             if n == a*b:
    #                 print("Yes")
    #                 self.eng += 3 + self.jewel * 2
    #                 self.coin += 4 + self.jewel * 2
    #             else:
    #                 print("No")
    #                 print(a*b)
    # 挖矿          
    def dig(self,plt):
        keys = list(plt.ndic.keys())
        values = []
        s = 0
        for k in keys:
            s += plt.ndic[k]
            values.append(s)
        got = {}
        for l in range(10):
            r = random.randint(1,1000)
            for i in range(len(values)):
                if values[i] > r:
                    self.cargoHold.setdefault(keys[i],0)
                    self.cargoHold[keys[i]] += 1
                    got.setdefault(keys[i],0)
                    got[keys[i]] += 1
                    break
        plt.cap-=10
        time.sleep(2)
        print(s_green+'[挖矿]:'+ s_reset +'获得 ',got, end='')
    
    #  售卖货物
    def sell(self,p, n1, n2=1):
        keys    = list(self.cargoHold.keys())
        qty     = self.cargoHold[keys[n1]]
        if n2 > qty:
            n2 = qty
        if n1 >= len(keys):
            n1 = len(keys)
        self.cargoHold[keys[n1]] -= n2
        coin = n2 * p[keys[n1]]
        # 卖出简报
        print(s_yellow+'[售卖]:',s_reset +'卖出'+ s_under ,keys[n1],'x',n2,'获得',coin ,'星币' + s_reset, end='')
        return coin

    # 生成飞船数据字典,用于保存
    def getData(self):
        data = {
            'name':self.name,
            'type':self.type,
             'eng':self.eng,
            'coin':self.coin,
            'jewel':self.jewel,
            'lev':self.lev,
            'cargoHold':self.cargoHold,
        }
        return data

class ActiveShip(Ship):
    def __init__(self, shipdata) -> None:
        super().__init__(shipdata)
        data = self.calculate()
        self.mx_energy = self.s_energy + data['energy']

    def calculate(self):
        data = {'reduce'}

